/* Controllers */
app.controller('homeCtrl',['$scope','$location', function ($scope,$location) {

}]);

// 报名页面
app.controller('signCtrl',['$scope','$location', '$timeout', 'FileUploader', '$http', '$interval', function ($scope,$location,$timeout,FileUploader,$http,$interval) {
    //选择上传图片
    $timeout(function () {
        try {
            document.querySelector('.img-ul .add').addEventListener("click", function(e){
                document.getElementById('choiceUpload').click();
            }, true);
        }catch (e){}
    },100);


    //定义导航
    $scope.navType = 'sign';

    // 定义表单数据
    $scope.users = new Object();

    // 数据
    var datas = {
        create : {
            declaration: '个人介绍+创业项目介绍(300字)',
            organizName: '计 划 书',
            organization: '提交项目计划概要大纲'
        },
        youth : {
            declaration: '个人介绍+青春宣言(300字内)',
            organizName: '所在院校',
            organization: '请输入您所在的院校'
        }
    }

    // 默认进入创客组
    $scope.title = 'youth';
    $scope.users.type = 2;
    $scope.formTxt = datas[$scope.title];


    // $scope.success = true;

    // 切换栏目
    $scope.changeTitle = function (value) {
        // if($scope.success) return;
        $scope.title = value;
        $scope.formTxt = datas[$scope.title];
        if(value=='create'){
            $scope.users.type = 1;
        }else if(value=='youth'){
            $scope.users.type = 2;
        }
    }

    //表单验证
    function formVerify() {
        if($scope.imgArray.length==0){
            $scope.showPrompt('请上传<br>至少一张图片！');
            return false;
        }
        if(!$scope.users.name){
            $scope.showPrompt('请输入<br>您的姓名！');
            return false;
        }
        if(!$scope.users.code){
            $scope.showPrompt('请输入<br>手机验证码！');
            return false;
        }
        if(!$scope.users.declaration){
            $scope.showPrompt('请输入<br>参赛宣言！');
            return false;
        }
        // 判断图片是否全部上传成功
        var isImgSuccess = true;
        for (var i in $scope.imgArray){
            if(!$scope.imgArray[i].url){
                isImgSuccess = false;
            }
        }
        if(!isImgSuccess){
            $scope.showPrompt('图片<br>正在上传中！');
            return false;
        }
        return true;
    }

    //确认报名
    $scope.confirmSubmit =function () {
        // 表单验证
        if(!formVerify()) return;

        // 预览报名信息
        window.scrollTo(0,0);
        $scope.success = 'success';
    }

    //重新填写
    $scope.reWrite = function () {
        window.scrollTo(0,0);
        $scope.success = false;
        $timeout(function () {
            try {
                document.querySelector('.img-ul .add').addEventListener("click", function(e){
                    document.getElementById('choiceUpload').click();
                }, true);
            }catch (e){}
        });
    }

    //提交报名信息
    var isconfirmSubmit = true;
    $scope.signSubmit = function () {
        // 请求接口锁
        if(!isconfirmSubmit) return;
            isconfirmSubmit = false;
        // 赋值上传图片数组
        var filesrc = [],tc_img = [];
        for (var i in $scope.imgArray){
            filesrc.push($scope.imgArray[i].file);
            tc_img.push($scope.imgArray[i].url);
        }

        //数据赋值
        var postData = {
            type : $scope.users.type,
            name : $scope.users.name,
            code : $scope.users.code,
            declaration : $scope.users.declaration,
            organization : $scope.users.organization,
            image : filesrc,
            tc_image : tc_img

        }
        console.log(postData);
        // 提交报名
         $http.post($config.host+'/index.php?s=/Zmhhr/add', postData).success(function(json) {
             if(json.success){
             // 报名成功
                 $scope.showPrompt('报名成功！',1000,function () {
                     $config.toTop(function () {
                         $scope.$apply(function () {
                             $scope.numbers = json.data.id;
                             $scope.success = 'complete';
                             $timeout(function () {
                                 $scope.showPrompt('关注活动官方公号，获取更多晋级信息和精彩内容',10000);
                                 //显示关注的动画提示图标
                                 $scope.attentShow = true;
                             },2000);
                         })
                     });

                 });
             }else{
                $scope.showPrompt(json.msg+',正在返回修改',3000,function () {
                    $scope.$apply(function () {
                        window.scrollTo(0,0);
                        $scope.success = false;
                    })
                });
             }
             // 解除请求接口锁
             isconfirmSubmit = true;
         }).error(function () {
             $scope.showPrompt('服务器故障，请重试！');
         })
    }

    // 初始化图片上传
    var uploader = $scope.uploader = new FileUploader({
        url: $config.host+'/index.php?s=/Upload/uploadZmhhr'
    });

    // 定义图片上传数组
    $scope.imgArray = [];
    $scope.imgUploadTxt = '添加照片（最多上传3张照片）';

    //图片筛选条件
    uploader.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });

    //图片不能大于5M
    /*uploader.filters.push({
        name: 'imageSize',
        fn: function(item /!*{File|FileLikeObject}*!/, options) {
            return item.size < 5000000;
        }
    });*/

    // 大于三个就移除
    var ImgTypeName = 0;    //上传图片的ID
    uploader.onAfterAddingFile = function(fileItem){
        if(fileItem.file.size > 10000000){
            $scope.showPrompt('上传图片不能大于10M');
            fileItem.remove();
        }else{
            if(uploader.queue.length>3){
                $scope.showPrompt('最多上传3张图片')
                fileItem.remove();
            }else {
                fileItem.file.typeName = ImgTypeName;
                $scope.imgArray.push({
                    name : fileItem.file.typeName
                })
                ImgTypeName++;
            }
        }
        // console.log(fileItem)
    };

    //确认上传
    uploader.onAfterAddingAll = function(fileItem){
        setImgUploadTxt();
        uploader.uploadAll();
    };

    //确认成功
    uploader.onSuccessItem = function(fileItem, response, status, headers){
        //赋值到本地保存
        for (var i in $scope.imgArray){
            // alert($scope.imgArray[i].name+'----'+(fileItem.file.typeName));
            if($scope.imgArray[i].name == fileItem.file.typeName){
                $scope.imgArray[i].url = response.TCthumb;
                $scope.imgArray[i].file = response.Filesrc;
            }
        }
    };

    // 移除单张图片
    $scope.removeImgItem = function (index) {
        // 移除数组中的值
        $scope.imgArray.splice(index,1);
        // 移除组件中的值
        uploader.queue[index].remove();
        setImgUploadTxt();
    }

    // 设置上传图片的文字提示
    function setImgUploadTxt() {
        var array = ['一','二','三'];
        if($scope.imgArray.length>0){
            $scope.imgUploadTxt = '已添加'+array[$scope.imgArray.length-1]+'张图片！（点击照片删除）';
        }else{
            $scope.imgUploadTxt = '添加照片（最多上传3张照片）';
        }
    }

}]);

// 其他页面
app.controller('otherCtrl',['$scope','$location', function ($scope,$location) {

}]);

// 列表页面
app.controller('listCtrl',['$scope','$location','$timeout','$http','$localStorage', function ($scope,$location,$timeout,$http,$localStorage) {
    window.scrollTo(0,0);
    $scope.wxshare(false);

    //定义导航
    $scope.navType = 'list';
    if($location.hash()){
        $scope.title = 'create';
    }else{
        // 默认进入创客组
        $scope.title = 'youth';
    }

    // 总人数
    $scope.count = 0;
    $http({
        method:'POST',
        url:'/index.php?s=/Zmhhr/rankingMsg',
    }).then(function(result){ //正确请求成功时处理
        $scope.count = result.data.data.enrollment;
    })

    // 加载列表
    $scope.loadlock = true;
    $scope.loaddata = function(type,page){
        if($scope.loadlock){
            if(type == 1){
                if(!$scope.listdata.bluelist.more) return;
            }else{
                if(!$scope.listdata.redlist.more) return;
            }
            $scope.loadlock = false;
            page++;
            $http({
                method:'POST',
                url:'/index.php?s=/Zmhhr/newRegistrationList',
                data:{type:type,page:page},
            }).then(function(result){ //正确请求成功时处理
                if(result.data.data.length > 0){
                    if(type == 1){
                        $scope.listdata.bluelist.data = $scope.listdata.bluelist.data.concat(result.data.data);
                        $scope.listdata.bluelist.page = page;
                    }else{
                        $scope.listdata.redlist.data = $scope.listdata.redlist.data.concat(result.data.data);
                        $scope.listdata.redlist.page = page;
                    }
                    if(result.data.data.length < 20){
                        if(type == 1){
                            $scope.listdata.bluelist.more = false;
                        }else{
                            $scope.listdata.redlist.more = false;
                        }
                    }
                }else{
                    if(type == 1){
                        $scope.listdata.bluelist.more = false;
                    }else{
                        $scope.listdata.redlist.more = false;
                    }
                }
                $scope.loadlock = true;
            })
        }
    };

    // 记录访问次数
    $scope.markTimes = function(){
        var type;
        if($scope.title == 'create'){
            type = 1;
        }else{
            type = 2;
        }
        $http({
            method: 'POST',
            url: '/index.php?s=/Zmhhr/setBrowse',
            data: {type: type},
        }).then(function (result) { //正确请求成功时处理
        });
    };

    // 初始化本地存储
    $scope.setLocalStorage = function(){
        $scope.listdata = $localStorage.listdata = {
            time:date,
            bluelist: {page: 0, more: true, data: []},
            redlist: {page: 0, more: true, data: []}
        };
        if($scope.title == 'create'){
            $scope.loaddata(1,$scope.listdata.bluelist.page);
        }else{
            $scope.loaddata(2,$scope.listdata.redlist.page);
        }
        $scope.markTimes();
    }

    // 初始化列表数据
    var date = new Date().getTime();
    if(!$localStorage.listdata){
        $scope.setLocalStorage();
    }else{
        var time = (date - $localStorage.listdata.time)/1000/60;
        if(time >= 3){
            $scope.setLocalStorage();
        }else{
            $scope.listdata = $localStorage.listdata;
            if($scope.title == 'create'){
                if($scope.listdata.bluelist.data){
                    $scope.loaddata(1,$scope.listdata.bluelist.page);
                }
            }else{
                if($scope.listdata.redlist.data){
                    $scope.loaddata(2,$scope.listdata.redlist.page);
                }
            }
            $scope.markTimes();
        }
    }

    // 切换栏目
    $scope.changeTitle = function (value) {
        $scope.title = value;
        $scope.sData.isSearch = false;
        $scope.sData.searchTxt = '';
        if (value != 'youth') {
            $scope.listdata.show = 'create';
            $location.hash('create');
            if ($scope.listdata.bluelist.data.length == 0) {
                $scope.loaddata(1, 0);
            }
        } else {
            $scope.listdata.show = 'youth';
            $location.hash('');
            if ($scope.listdata.redlist.data.length == 0) {
                $scope.loaddata(2, 0);
            }
        }
        $scope.markTimes();
    }

    // 滚动加载
    window.onscroll = function () {
        if(document.querySelector('.'+$scope.title+"more")){
            var top = window.screen.availHeight + document.body.scrollTop;
            var more = document.querySelector("."+$scope.title+"more").getBoundingClientRect().top+document.body.scrollTop;
            // $localStorage.listdata.scrolltop = document.body.scrollTop;
            if (top >= more) {
                if($scope.title == 'create'){
                    $scope.loaddata(1,$scope.listdata.bluelist.page);
                }else{
                    $scope.loaddata(2,$scope.listdata.redlist.page);
                }
            }
        }
    }

    // 搜索
    $scope.sData = {
        isSearch : false,
        searchTxt : '',
        searchlock: true,
        nothing: false,
        data:[]
    }
    $scope.toSearch = function(){
        var type;
        if($scope.sData.searchTxt == "") return;
        if($scope.sData.searchlock){
            $scope.sData.searchlock = false;
            if($scope.title == 'create'){
                type = 1;
            }else{
                type = 2;
            }
            $http({
                method:'POST',
                url:'/index.php?s=/Zmhhr/newRegistrationList',
                data:{type:type,key:$scope.sData.searchTxt},
            }).then(function(result){ //正确请求成功时处理
                $scope.sData.isSearch = true;
                $scope.sData.data = [];
                if(result.data.data.length > 0){
                    $scope.sData.nothing = false;
                    $scope.sData.data = $scope.sData.data.concat(result.data.data);
                }else{
                    $scope.sData.nothing = true;
                }
                $scope.sData.searchlock = true;
            })
        }
    };
}]);

// 列表页面
app.controller('listlistCtrl',['$scope','$location','$timeout','$interval','$http','$localStorage', 'Context', function ($scope,$location,$timeout,$interval,$http,$localStorage,Context) {
    window.scrollTo(0,0);
    $scope.wxshare(false);

    //倒计时
    /*var nowday = new Date().getTime();  //当前时间（毫秒）
    var lastday = new Date('2017/05/31').getTime(); //最后日期（毫秒）
    $scope.overday = parseInt((lastday-nowday)/1000/60/60/24);   //天数
    $scope.overHour = '99';
    $scope.overMinutes = '99';
    $scope.overSecond = '99';
    $interval(function(){
        var nowday = new Date().getTime();  //当前时间（毫秒）
        $scope.overHour = parseInt((lastday-nowday)/1000/60/60%24);   //时
        $scope.overMinutes = parseInt((lastday-nowday)/1000/60%60);   //分
        $scope.overSecond = parseInt((lastday-nowday)/1000%60);   //秒
        if($scope.overHour < 10){
            $scope.overHour = '0'+$scope.overHour;
        }
        if($scope.overMinutes < 10){
            $scope.overMinutes = '0'+$scope.overMinutes;
        }
        if($scope.overSecond < 10){
            $scope.overSecond = '0'+$scope.overSecond;
        }
    },1000);*/

    //定义导航
    $scope.navType = 'list';
    if($location.hash()){
        $scope.title = 'create';
    }else{
        // 默认进入创客组
        $scope.title = 'youth';
    }
    //默认进入最新参赛
    $scope.minititle = 'new';

    // 总人数，投票人数
    $scope.count = 0;
    $http({
        method:'POST',
        url:$config.host+'/index.php?s=/Zmhhr/rankingMsg',
    }).then(function(result){ //正确请求成功时处理
        $scope.count = result.data.data.enrollment;
        $scope.voter = result.data.data.voter;
    })

    // 加载列表
    $scope.loadlock = true;
    $scope.loaddata = function(){
        var _this,type,url;
        if($scope.title == 'create'){
            type = 1;
            _this = $scope.listdata.bluelist
        }else{
            type = 2;
            _this = $scope.listdata.redlist
        }
        if($scope.minititle == 'new'){
            _this = _this.new;
            url = '/index.php?s=/Zmhhr/newRegistrationList';
        }else{
            _this = _this.top;
            url = '/index.php?s=/Zmhhr/voteRankingList';
        }
        if(!_this.more) return;
        if($scope.loadlock){
            $scope.loadlock = false;
            _this.page++;
            $http({
                method:'POST',
                url:$config.host+url,
                data:{type:type,page:_this.page},
            }).then(function(result){ //正确请求成功时处理
                var data = result.data.data;
                if(data.length > 0){
                    _this.data = _this.data.concat(data);
                    _this.page = _this.page;
                    if(data.length < 20){
                        _this.more = false;
                    }
                }else{
                    _this.more = false;
                }
                $scope.thislist = _this;
                $scope.loadlock = true;
            })
        }
    };

    // 记录访问次数
    $scope.browse = 0;
    $scope.markTimes = function(){
        var listtype;
        if($scope.title == 'create'){
            listtype = 1;
        }else{
            listtype = 2;
        }
        $http({
            method: 'POST',
            url: $config.host+'/index.php?s=/Zmhhr/setBrowse',
            data: {type: listtype},
        }).then(function (result) { //正确请求成功时处理
            if(listtype == 1){
                $scope.browse = result.data.type1;
            }else{
                $scope.browse = result.data.type2;
            }
        });
    };

    // 初始化本地存储
    $scope.setLocalStorage = function(){
        $scope.listdata = $localStorage.listlistdata = {
            time:date,
            bluelist: {new:{page: 0, more: true, data: []},top:{page: 0, more: true, data: []}},
            redlist: {new:{page: 0, more: true, data: []},top:{page: 0, more: true, data: []}}
        };
        if($scope.title == 'create'){
            _this = $scope.listdata.bluelist
        }else{
            _this = $scope.listdata.redlist
        }
        if($scope.minititle == 'new'){
            _this = _this.new;
        }else{
            _this = _this.top;
        }
        $scope.thislist = _this;
        $scope.loaddata();
        $scope.markTimes();
    }

    // 初始化列表数据
    var date = new Date().getTime();
    if(!$localStorage.listlistdata){
        $scope.setLocalStorage();
    }else{
        var time = (date - $localStorage.listlistdata.time)/1000/60;
        if(time >= 3){
            $scope.setLocalStorage();
        }else{
            var _this;
            $scope.listdata = $localStorage.listlistdata;
            if($scope.title == 'create'){
                _this = $scope.listdata.bluelist
            }else{
                _this = $scope.listdata.redlist
            }
            if($scope.minititle == 'new'){
                _this = _this.new;
            }else{
                _this = _this.top;
            }
            if(_this.data.length == 0){
                $scope.loaddata();
            }
            $scope.thislist = _this;
            $scope.markTimes();
        }
    }

    // 切换栏目
    $scope.changeTitle = function (type,value) {
        if(type == 1){
            $scope.title = value;
        }else{
            $scope.minititle = value;
        }
        $scope.sData.isSearch = false;
        $scope.sData.searchTxt = '';

        var _this;
        if ($scope.title == 'create') {
            $location.hash('create');
            _this = $scope.listdata.bluelist;
        } else {
            $location.hash('');
            _this = $scope.listdata.redlist;
        }
        if($scope.minititle == 'new'){
            _this = _this.new;
        }else{
            _this = _this.top;
        }
        if (_this.data.length == 0) {
            $scope.loaddata();
        }
        $scope.thislist = _this;
        $scope.markTimes();
        // console.log(_this);
        // console.log($scope.listdata);
    }

    // 切换迷你栏目
    $scope.changeMiniTitle = function (value) {
        $scope.minititle = value;
        $scope.sData.isSearch = false;
        $scope.sData.searchTxt = '';
        if ($scope.title == 'create') {
            _this = $scope.listdata.bluelist;
        } else {
            _this = $scope.listdata.redlist;
        }
        if(value == 'new'){
            _this = _this.new;
        }else{
            _this = _this.top;
        }
        if (_this.data.length == 0) {
            $scope.loaddata();
        }
        $scope.thislist = _this;
        // console.log(_this);
        // console.log($scope.listdata);
    }

    // 滚动加载
    window.onscroll = function () {
        if(document.querySelector('.loadmore')){
            var top = window.screen.availHeight + document.body.scrollTop;
            var more = document.querySelector(".loadmore").getBoundingClientRect().top+document.body.scrollTop;
            if (top >= more) {
                $scope.loaddata();
            }
        }
    }

    // 搜索
    $scope.sData = {
        isSearch : false,
        searchTxt : '',
        searchlock: true,
        nothing: false,
        data:[]
    }
    $scope.toSearch = function(){
        var type;
        if($scope.sData.searchTxt == "") return;
        if($scope.sData.searchlock){
            $scope.sData.searchlock = false;
            if($scope.title == 'create'){
                type = 1;
            }else{
                type = 2;
            }
            $http({
                method:'POST',
                url:$config.host+'/index.php?s=/Zmhhr/newRegistrationList',
                data:{type:type,key:$scope.sData.searchTxt},
            }).then(function(result){ //正确请求成功时处理
                $scope.sData.isSearch = true;
                $scope.sData.data = [];
                if(result.data.data.length > 0){
                    $scope.sData.nothing = false;
                    $scope.sData.data = $scope.sData.data.concat(result.data.data);
                }else{
                    $scope.sData.nothing = true;
                }
                $scope.sData.searchlock = true;
            })
        }
    };

    var isClickVote = true;
    $(document).on('click','.votebtn',function(){
        var _this = $(this);
        var id = $(this).data('id');
        if(!isClickVote) return;
        isClickVote = false;
        if(!Context.isLogin()){
            location.href = 'http://m.mp.oeeee.com/user.php?m=WXOAuth2&url2go='+encodeURIComponent(location.href)+'&a=infoScope&regchannel=h5_praise&account_key=weixin_h5';
            return;
        }
        $scope.$apply(function(){
            $http({
                method : 'GET',
                url : $config.host+'/index.php?s=/Zmhhr/vote',
                params : {
                    id : id
                }
            }).success(function (json) {
                if(json.success){
                    var obj = $('<div class="animated">+1</div>');
                    _this.after(obj);
                    obj.addClass('fadeOutUp');
                    $timeout(function(){
                        angular.forEach($scope.thislist.data, function(data){
                            if(data.id == id){
                                data.vote++;
                            }
                        });
                    },500);
                }else {
                    if(json.code == 0){
                        $scope.showPrompt(json.msg);
                    }else{
                        $scope.showAlertBox();
                    }
                }
                isClickVote = true;
            })
        });
    });
}]);

// 海报页面
app.controller('posterCtrl',['$scope','$location','$stateParams', function ($scope,$location,$stateParams) {
    $scope.imgsrc = 'http://h5.oeeee.com/'+decodeURIComponent($stateParams.src).slice(1);
    $scope.wxshare(true,$stateParams.type);
}]);

//复赛
app.controller('rematchCtrl',['$scope','$location','$timeout','$interval','$http','$localStorage', 'Context', function ($scope,$location,$timeout,$interval,$http,$localStorage,Context) {
    window.scrollTo(0,0);
    $scope.wxshare(false);

    //定义导航
    $scope.navType = 'rematch';

    if($location.hash()){
        $scope.title = 'create';
    }else{
        // 默认进入创客组
        $scope.title = 'youth';
    }
    //默认进入投票排行
    $scope.minititle = 'top';

    // 总人数，投票人数
    $scope.count = 0;
    $http({
        method:'POST',
        url:$config.host+'/index.php?s=/Zmhhr/rankingMsg',
    }).then(function(result){ //正确请求成功时处理
        $scope.count = result.data.data.enrollment;
    })
    $http({
        method:'POST',
        url:$config.host+'/index.php?s=/Zmhhr/rankingMsg/&i=2',
    }).then(function(result){ //正确请求成功时处理
        $scope.voter = result.data.data.voter;
    })

    // 加载列表
    $scope.loadlock = true;
    $scope.loaddata = function(){
        var _this,type,url;
        if($scope.title == 'create'){
            type = 1;
            _this = $scope.relistdata.bluelist
        }else{
            type = 2;
            _this = $scope.relistdata.redlist
        }
        if($scope.minititle == 'new'){
            _this = _this.new;
            url = '/index.php?s=/Zmhhr/newRegistrationList';
        }else{
            _this = _this.top;
            url = '/index.php?s=/Zmhhr/voteRankingList/&i=2';
        }
        if(!_this.more) return;
        if($scope.loadlock){
            $scope.loadlock = false;
            _this.page++;
            $http({
                method:'POST',
                url:$config.host+url,
                data:{type:type,page:_this.page},
            }).then(function(result){ //正确请求成功时处理
                var data = result.data.data;
                if(data.length > 0){
                    _this.data = _this.data.concat(data);
                    _this.page = _this.page;
                    if(data.length < 20){
                        _this.more = false;
                    }
                }else{
                    _this.more = false;
                }
                $scope.thislist = _this;
                $scope.loadlock = true;
            })
        }
    };

    // 记录访问次数
    $scope.browse = 0;
    $scope.markTimes = function(){
        var listtype;
        if($scope.title == 'create'){
            listtype = 1;
        }else{
            listtype = 2;
        }
        $http({
            method: 'POST',
            url: $config.host+'/index.php?s=/Zmhhr/setBrowse',
            data: {type: listtype},
        }).then(function (result) { //正确请求成功时处理
            if(listtype == 1){
                $scope.browse = result.data.type1;
            }else{
                $scope.browse = result.data.type2;
            }
        });
    };

    // 初始化本地存储
    $scope.setLocalStorage = function(){
        $scope.relistdata = $localStorage.relistlistdata = {
            time:date,
            bluelist: {new:{page: 0, more: true, data: []},top:{page: 0, more: true, data: []}},
            redlist: {new:{page: 0, more: true, data: []},top:{page: 0, more: true, data: []}}
        };
        if($scope.title == 'create'){
            _this = $scope.relistdata.bluelist
        }else{
            _this = $scope.relistdata.redlist
        }
        if($scope.minititle == 'new'){
            _this = _this.new;
        }else{
            _this = _this.top;
        }
        $scope.thislist = _this;
        $scope.loaddata();
        $scope.markTimes();
    }

    // 初始化列表数据
    var date = new Date().getTime();
    if(!$localStorage.relistlistdata){
        $scope.setLocalStorage();
    }else{
        var time = (date - $localStorage.relistlistdata.time)/1000/60;
        if(time >= 3){
            $scope.setLocalStorage();
        }else{
            var _this;
            $scope.relistdata = $localStorage.relistlistdata;
            if($scope.title == 'create'){
                _this = $scope.relistdata.bluelist
            }else{
                _this = $scope.relistdata.redlist
            }
            if($scope.minititle == 'new'){
                _this = _this.new;
            }else{
                _this = _this.top;
            }
            if(_this.data.length == 0){
                $scope.loaddata();
            }
            $scope.thislist = _this;
            $scope.markTimes();
        }
    }

    // 切换栏目
    $scope.changeTitle = function (type,value) {
        if(type == 1){
            $scope.title = value;
        }else{
            $scope.minititle = value;
        }
        $scope.sData.isSearch = false;
        $scope.sData.searchTxt = '';

        var _this;
        if ($scope.title == 'create') {
            $location.hash('create');
            _this = $scope.relistdata.bluelist;
        } else {
            $location.hash('');
            _this = $scope.relistdata.redlist;
        }
        if($scope.minititle == 'new'){
            _this = _this.new;
        }else{
            _this = _this.top;
        }
        if (_this.data.length == 0) {
            $scope.loaddata();
        }
        $scope.thislist = _this;
        $scope.markTimes();
        // console.log(_this);
        // console.log($scope.listdata);
    }

    // 切换迷你栏目
    $scope.changeMiniTitle = function (value) {
        $scope.minititle = value;
        $scope.sData.isSearch = false;
        $scope.sData.searchTxt = '';
        if ($scope.title == 'create') {
            _this = $scope.listdata.bluelist;
        } else {
            _this = $scope.listdata.redlist;
        }
        if(value == 'new'){
            _this = _this.new;
        }else{
            _this = _this.top;
        }
        if (_this.data.length == 0) {
            $scope.loaddata();
        }
        $scope.thislist = _this;
        // console.log(_this);
        // console.log($scope.listdata);
    }

    // 滚动加载
    window.onscroll = function () {
        if(document.querySelector('.loadmore')){
            var top = window.screen.availHeight + document.body.scrollTop;
            var more = document.querySelector(".loadmore").getBoundingClientRect().top+document.body.scrollTop;
            if (top >= more) {
                $scope.loaddata();
            }
        }
    }

    // 搜索
    $scope.sData = {
        isSearch : false,
        searchTxt : '',
        searchlock: true,
        nothing: false,
        data:[]
    }
    $scope.toSearch = function(){
        var type;
        if($scope.sData.searchTxt == "") return;
        if($scope.sData.searchlock){
            $scope.sData.searchlock = false;
            if($scope.title == 'create'){
                type = 1;
            }else{
                type = 2;
            }
            $http({
                method:'POST',
                url:$config.host+'/index.php?s=/Zmhhr/newRegistrationList',
                data:{type:type,key:$scope.sData.searchTxt},
            }).then(function(result){ //正确请求成功时处理
                $scope.sData.isSearch = true;
                $scope.sData.data = [];
                if(result.data.data.length > 0){
                    $scope.sData.nothing = false;
                    $scope.sData.data = $scope.sData.data.concat(result.data.data);
                }else{
                    $scope.sData.nothing = true;
                }
                $scope.sData.searchlock = true;
            })
        }
    };

    var isClickVote = true;
    $(document).on('click','.votebtn',function(){
        var _this = $(this);
        var id = $(this).data('id');
        if(!isClickVote) return;
        isClickVote = false;
        if(!Context.isLogin()){
            location.href = 'http://m.mp.oeeee.com/user.php?m=WXOAuth2&url2go='+encodeURIComponent(location.href)+'&a=infoScope&regchannel=h5_praise&account_key=weixin_h5';
            return;
        }
        $scope.$apply(function(){
            $http({
                method : 'GET',
                url : $config.host+'/index.php?s=/Zmhhr/vote/&i=2',
                params : {
                    id : id
                }
            }).success(function (json) {
                if(json.success){
                    var obj = $('<div class="animated">+1</div>');
                    _this.after(obj);
                    obj.addClass('fadeOutUp');
                    $timeout(function(){
                        angular.forEach($scope.thislist.data, function(data){
                            if(data.id == id){
                                data.vote++;
                            }
                        });
                    },500);
                }else {
                    if(json.code == 0){
                        $scope.showPrompt(json.msg);
                    }else{
                        $scope.showAlertBox();
                    }
                }
                isClickVote = true;
            })
        });
    });
}]);
